package com.youaishujuhui.devops.web.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.impl.SimpleDataSource;

import com.alibaba.druid.util.StringUtils;
import com.youaishujuhui.devops.web.domain.channel.Channel;
import com.youaishujuhui.devops.web.domain.game.Game;
import com.youaishujuhui.devops.web.domain.game.GameProcess;
import com.youaishujuhui.devops.web.domain.host.Host;

public class MigrationTool {
	
	public static void migrationHost(Dao dao, Dao dao1) {
		// 先清空
		dao1.clear(Host.class);
				
		// 获取老的机器表的所有数据
		List<Record> machines = dao.query("app_machine_info", null);
		for (Record record : machines) {
			int hostId = record.getInt("id");
			String property = record.getString("property");
			String idc = record.getString("idc_room");
			String area = record.getString("area");
			String ctccIp = record.getString("ctcc_ip");
			String cuccIp = record.getString("cucc_ip");
			String cmccIp = record.getString("cmcc_ip");
			String lanIp = record.getString("lan_ip");
			int sshPort = record.getInt("ssh_port");
			int curGameServerCount = record.getInt("game_server_count");
			int maxGameServerCount = record.getInt("max_game_server_count");
			int state = record.getInt("status");
			String remark = record.getString("remark");
			long createTime = record.getLong("create_time");
			int role = record.getInt("role");
			int project = record.getInt("project");
			
			if (role != 2) {
				curGameServerCount = maxGameServerCount = 0;
			}
			
			if (StringUtils.isEmpty(cmccIp)) {
				cmccIp = null;
	    	}
	    	
	    	if (StringUtils.isEmpty(ctccIp)) {
	    		ctccIp = null;
	    	}
	    	
	    	if (StringUtils.isEmpty(cuccIp)) {
	    		cuccIp = null;
	    	}
	    	
	    	if (StringUtils.isEmpty(remark)) {
	    		remark = null;
	    	}
	    	
	    	if (state == 1) {
	    		state = 0;
	    	} else {
	    		state = 1;
	    	}
			
	    	Chain ch = Chain.make("hostId",hostId);
			ch.add("property", property);
			ch.add("idc", idc);
			ch.add("area", area);
			ch.add("ctccIp", ctccIp);
			ch.add("cuccIp", cuccIp);
			ch.add("cmccIp", cmccIp);
			ch.add("lanIp", lanIp);
			ch.add("sshPort", sshPort);
			ch.add("curGameServerCount", curGameServerCount);
			ch.add("maxGameServerCount", maxGameServerCount);
			ch.add("state", state);
			ch.add("remark", remark);
			ch.add("role", role);
			ch.add("gameId", project);
			
			Date date = new Date(createTime * 1000);
			
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");  
		    String dateString = formatter.format(date);  
			
			ch.add("createTime", dateString);
			
			dao1.insert("t_host", ch);
		}

	}
	
	public static void migrationGame(Dao dao, Dao dao1) {
		
		// 先清空
		dao1.clear(Game.class);
		

		// 获取老的游戏表的所有数据
		List<Record> games = dao.query("app_project_name", null);
		for (Record record : games) {
			int gameId = record.getInt("id");
			String nameCn = record.getString("project_name_zh");
			String nameEn = record.getString("project_name_en");
			String packageDir = record.getString("package_name");
			String logDbName = record.getString("log_db_name");
			String gameDbName = record.getString("game_db_name");
			String domain = record.getString("domain");
			
			Chain ch = Chain.make("gameId",gameId);
			ch.add("deptId", 1);
			ch.add("nameCn", nameCn);
			ch.add("nameEn", nameEn);
			ch.add("packageDir", packageDir);
			ch.add("logDbName", logDbName);
			ch.add("gameDbName", gameDbName);
			ch.add("domain", domain);
			
			dao1.insert("t_game", ch);
		}
					
	}
	
	public static void migrationChannel(Dao dao, Dao dao1) {
		
		// 先清空
		dao1.clear(Channel.class);
		

		// 获取老的游戏表的所有数据
		List<Record> games = dao.query("app_channel_name", null);
		for (Record record : games) {
	
			int channelId = record.getInt("id");
			int gameId = record.getInt("project");
			String nameCn = record.getString("channel_name_zh");
			String nameEn = record.getString("channel_name_en");
			String property = record.getString("machine_provide");
			
			Chain ch = Chain.make("channelId",channelId);
			ch.add("gameId", gameId);
			ch.add("nameCn", nameCn);
			ch.add("nameEn", nameEn);
			ch.add("property", property);
			
			dao1.insert("t_channel", ch);
		}		
	}
	
	public static void migrationGameProcess(Dao dao, Dao dao1) {
		// 先清空
		dao1.clear(GameProcess.class);
		
		// 获取老的游戏表的所有数据
		List<Record> games = dao.query("app_game_info", null);
		for (Record record : games) {
			int gpId = record.getInt("id");
			int hostId = record.getInt("machine");
			int gameId = record.getInt("project");
			
			Host host = dao1.fetch(Host.class, Cnd.where("hostId", "=", hostId));
			if (host == null) {
				continue;
			}
			
			Game game = dao1.fetch(Game.class, Cnd.where("gameId", "=", gameId));
			if (game == null) {
				continue;
			}
			
			String channelName = record.getString("game_channel");
			int channelId = -1;
			Channel channel = dao1.fetch(Channel.class, Cnd.where("nameEn", "=", channelName));
			
			if (channel == null) {
				continue;
			} else {
				channelId = channel.getChannelId();
			}
			
			String dir = record.getString("game_dir");
			int serverId = record.getInt("game_server_id");
			int gamePort = record.getInt("game_port");
			int gmPort = record.getInt("game_gm_port");
			
			String domain = record.getString("game_domain");
			Integer state = record.getInt("game_status");
			long createTime = record.getLong("game_create_time");
			String name = record.getString("game_server_name");
			Timestamp offTime = record.getTimestamp("server_close_date");
			
			if (StringUtils.isEmpty(name)) {
				name = null;
			}
			
			if (StringUtils.isEmpty(domain)) {
				domain = null;
			}
			
			int game_close_status = record.getInt("game_close_status");
			
			state = (game_close_status == 1 || offTime != null) ? 0 : 1;
			
			Chain ch = Chain.make("gpId", gpId);
			ch.add("hostId", hostId);
			ch.add("gameId", gameId);
			ch.add("dir", dir);
			
			ch.add("serverId", serverId);
			ch.add("channelId", channelId);
			ch.add("gamePort", gamePort);
			ch.add("gmPort", gmPort);
			
			ch.add("domain", domain);
			ch.add("state", state);
			
			Date date = new Date(createTime * 1000);
			
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");  
		    String dateString = formatter.format(date);  
			
			ch.add("createTime", dateString);
			ch.add("name", name);
			ch.add("offTime", offTime);
			
			dao1.insert("t_game_process", ch);
		}
	}
	
	public static void main(String [] args) {
		SimpleDataSource dataSource = new SimpleDataSource();
		dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1/study_mysql_test?useSSL=false&characterEncoding=UTF-8");
		dataSource.setUsername("root");
		dataSource.setPassword("qweasdzxc");

		Dao dao = new NutDao(dataSource);
		
		// 创建一个数据源
		SimpleDataSource dataSource1 = new SimpleDataSource();
		dataSource1.setJdbcUrl("jdbc:mysql://127.0.0.1/devops_web?useSSL=false&characterEncoding=UTF-8");
		dataSource1.setUsername("root");
		dataSource1.setPassword("qweasdzxc");

		// 创建一个NutDao实例,在真实项目中, NutDao通常由ioc托管, 使用注入的方式获得.
		Dao dao1 = new NutDao(dataSource1);
		
		migrationGame(dao, dao1);
		migrationHost(dao, dao1);
		migrationChannel(dao, dao1);
		migrationGameProcess(dao, dao1);

	}
}
